package com.kong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.kong.constant.MessageConstant;
import com.kong.dao.MemberDao;
import com.kong.dao.OrderDao;
import com.kong.dao.OrderSettingDao;
import com.kong.pojo.Member;
import com.kong.pojo.Order;
import com.kong.pojo.OrderSetting;
import com.kong.service.OrderService;
import com.kong.utils.DateUtils;
import com.kong.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author yang2013
 * @data 2020/10/15 16:19
 */
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;


    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Order submitOrder(Map<String, Object> map) throws Exception {
        //查询当天是否可以预约
        String date= map.get("orderDate").toString();
        Date orderDate= DateUtils.parseString2Date(date);
        OrderSetting orderSetting=orderSettingDao.findByDate(orderDate);
        if (orderSetting==null){
            throw new RuntimeException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //判断是否为会员
        String telephone= map.get("telephone").toString();
        Member byTelephone = memberDao.findByTelephone(telephone);
        //获取查询预约表所需要参数--封装到 order
        String id=map.get("setmealId").toString();
        Integer setmealId= Integer.parseInt(id);
        String orderType= (String) map.get("orderType");
        Order order=new Order(orderDate,orderType,setmealId);
        if (!(byTelephone==null)){
            //如果是会员就判断是否已经预约
            order.setMemberId(byTelephone.getId());
            int count=orderDao.findCountByCondition(order);
            if (count>0){
                throw new RuntimeException(MessageConstant.HAS_ORDERED);
            }
        }else {
            //插入会员表--成功后将id赋值给order
            Member member=setMember(map);
            memberDao.add(member);
            order.setMemberId(member.getId());
        }
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //判断预约是否已满
        if (orderSetting.getNumber()==orderSetting.getReservations()){
            throw new RuntimeException(MessageConstant.ORDER_FULL);
        }
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.updataOrderSetting(orderSetting);

        //更新t_order表
        orderDao.add(order);

        //发送成功通知
        if (false){
            SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,date);
        }
        return order;
    }

    @Override
    public Map<String, Object> findDetailById(Integer id) {
        return orderDao.findById4Detail(id);
    }

    @Override
    public Map getSetmealReport() {
        Map map=new HashMap();
        List<Map> setmealCount=orderDao.getSetmealRepot();

        List<String> setmealNames=new ArrayList<>();
        for (Map map1 : setmealCount) {
            String name = map1.get("name").toString();
            setmealNames.add(name);
        }

        map.put("setmealCount",setmealCount);
        map.put("setmealNames",setmealNames);
        return map;

    }


    /**
     * 装配member对象
     */
    private Member setMember(Map<String, Object> map) {
        Member member=new Member();
        member.setName((String) map.get("name"));
        member.setSex((String) map.get("sex"));
        member.setIdCard((String) map.get("idCard"));
        member.setPhoneNumber((String) map.get("telephone"));
        member.setRegTime(new Date());
        return member;
    }
}
